package com.hyc.myvideo.ui.activity.my;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.acmenxd.toaster.Toaster;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.Code;
import com.hyc.myvideo.ui.base.BaseFActivity;
import com.hyc.myvideo.widget.ClearEditText;
import com.hyc.utils.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;

import static com.hyc.utils.StringUtils.isEmail;
import static com.hyc.utils.StringUtils.isPhoneNumber;


/**
 * 建议反馈
 * @author gyb
 * @version 1.0
 * @created 2017/07/13
 */
public class SuggestActivity extends BaseFActivity {
    /*建议内容*/
    @BindView(R.id.e_suggest_content_id)
    ClearEditText editTextContent;
    /*内容记数*/
    @BindView(R.id.t_suggest_content_number_id)
    TextView textViewNumber;
    /*联系方式*/
    @BindView(R.id.e_suggest_phone_id)
    ClearEditText editTextContact;
    /**提交按钮*/
    @BindView(R.id.b_submit_id)
    Button buttonSubmit;
    @Override
    public int getContentViewId() {
        return R.layout.activity_suggest;
    }

    @Override
    protected void initView() {
        setLeftBtnClick(true,getStr(R.string.huc_head_suggest));
        setButtonSubmit(false);
        textViewNumber.setText(0 + "/" + 500);
        editTextContent.addTextChangedListener(textWatcher);
    }
    @Override
    protected void initData() {

    }

    @Override
    protected void oNext(String r, String method) {
        BaseResult<Code> result = JSONObject.parseObject(r, new TypeReference<BaseResult<Code>>(){});
        if(result.isCode()){
           this.finish();
        }
        Toaster.show(result.getMsg());
    }

    @Override
    protected void oError() {

    }

    @OnClick({R.id.b_submit_id})
    public void onClicks(View view) {
        switch (view.getId()){
            /*提交*/
            case R.id.b_submit_id:
                if(isVerification()){
                    api.userFeedback(getBEDstr(editTextContent),getBEDstr(editTextContact));
                }
                break;
        }
    }

    /***
     * 设置提交按钮状态
     * @param isClickable
     */
    private void setButtonSubmit(boolean isClickable){
        buttonSubmit.setClickable(isClickable);
        if(isClickable){
            buttonSubmit.setBackgroundResource(R.drawable.button_shape_1);
        }else{
            buttonSubmit.setBackgroundResource(R.drawable.button_shape_0);
        }
    }
    /**
     * 字数监听
     */
    TextWatcher textWatcher = new TextWatcher() {
        @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
        @Override public void afterTextChanged(Editable s) {
            int num = s.toString().length();
            textViewNumber.setText(num + "/" + (500 - num));
            if(num >= 10){
                setButtonSubmit(true);
            }else {
                setButtonSubmit(false);
            }
        }
    };

    /**
     * 验证信息
     */
    private boolean isVerification() {
        boolean isb = false;
        String content = getBEDstr(editTextContent);
        String contact = getBEDstr(editTextContact);
        if (StringUtils.isEmpty(content)) {
            Toaster.show("反馈信息不能为空");
            return isb;
        } else if (content.length() < 10) {
            Toaster.show("反馈意见不能小于10个字");
            return isb;
        }
        if (!StringUtils.isEmpty(contact)) {
            if (contact.contains("@")) {
                if (!isEmail(contact)) {
                    Toaster.show("输入的邮箱不合法!");
                    return isb;
                }
            } else {
                if (!isPhoneNumber(contact)) {
                    Toaster.show("输入的手机号不合法!");
                    return isb;
                }
            }
        }
        isb = true;
        return isb;
    }


}
